package com.admin.system.dao;

import com.admin.model.pojo.Dept;
import com.admin.model.pojo.Job;
import com.admin.model.pojo.Role;
import com.admin.model.pojo.User;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Set;

/**
 * (User)表数据库访问层
 *
 * @author makejava
 * @since 2021-01-31 19:19:29
 */
public interface UserDao extends BaseMapper<User> {
    /**
     * 部门id查询
     * @param deptId
     * @return
     */
    @Select("select * from sys_user where id in(select user_id from sys_user_dept  where dept_id=#{deptId})")
    List<User> queryByDeptId(Long deptId);

    /**
     * id查询用户
     * @param userId
     * @return
     */
    @Select("select * FROM sys_user WHERE id = #{userId}")
    User queryById(Long userId);

    /**
     * 条件查询
     *
     * @param user
     * @return
     */
    List<User> queryAll(User user);

    /**
     * 授权用户角色
     *
     * @param userId
     * @param roleId
     * @return
     */
    @Insert("insert into sys_user_role (user_id,role_id)values(#{userId},#{roleId})")
    int authRole(Long userId, Long roleId);

    /**
     * 授权用户角色是否存在
     * @param userId
     * @param roleId
     * @return
     */
    @Select("select 1 from sys_user_role WHERE user_id=#{userId} and role_id=#{roleId} LIMIT 1")
    Boolean existAuthRole(Long userId, Long roleId);

    /**
     * 删除用户授权角色
     * @param userId
     * @return
     */
    @Delete("delete from sys_user_role where user_id=#{userId}")
    Boolean delAuthRoleByUserId(Long userId);

    /**
     * 删除用户授权角色
     * @param roleId
     * @return
     */
    @Delete("delete from sys_user_role where role_id=#{roleId}")
    Boolean delAuthRoleByRoleId(Long roleId);

    /**
     * 授权用户部门
     * @param userId
     * @param deptId
     * @return
     */
    @Insert("insert into sys_user_dept (user_id,dept_id)values(#{userId},#{deptId})")
    int authDept(Long userId, Long deptId);

    /**
     * 授权用户部门是否存在
     * @param userId
     * @param deptId
     * @return
     */
    @Select("select 1 from sys_user_dept WHERE user_id=#{userId} and role_id=#{deptId} LIMIT 1")
    Boolean existAuthDept(Long userId, Long deptId);

    /**
     * 删除用户授权部门
     * @param userId
     * @return
     */
    @Delete("delete from sys_user_dept where user_id=#{userId}")
    Boolean delAuthDeptByUserId(Long userId);

    /**
     * 删除用户授权部门
     * @param deptId
     * @return
     */
    @Delete("delete from sys_user_dept where dept_id=#{deptId}")
    Boolean delAuthDeptByDeptId(Long deptId);

    /**
     * 授权用户岗位
     * @param userId
     * @param jobId
     * @return
     */
    @Insert("insert into sys_user_job (user_id,job_id)values(#{userId},#{jobId})")
    int authJob(Long userId, Long jobId);

    /**
     * 授权用户岗位是否存在
     * @param userId
     * @param jobId
     * @return
     */
    @Select("select 1 from sys_user_job WHERE user_id=#{userId} and role_id=#{jobId} LIMIT 1")
    Boolean existAuthJob(Long userId, Long jobId);

    /**
     * 删除用户授权岗位
     * @param userId
     * @return
     */
    @Delete("delete from sys_user_job where user_id=#{userId}")
    Boolean delAuthJobByUserId(Long userId);

    /**
     * 删除用户授权岗位
     * @param jobId
     * @return
     */
    @Delete("delete from sys_user_job where job_id=#{jobId}")
    Boolean delAuthJobByJobId(Long jobId);


}